// pages/my/my.js
import {
  postRequest,
  getRequest
} from '../../api/request';
import {
  editTabBar,
} from '../../utils/util';
const app = getApp()
console.log(app);
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    myIndexTopBg: app.globalData.myIndexTopBg,
    myIndexHeadImg1: app.globalData.myIndexHeadImg1,
    myIndexHeadImg2: app.globalData.myIndexHeadImg2,
    showStyle: 0,
    iconList: [{
      icon: 'icon-a-VIPhuiyuanhuiyuanka',
      color: 'black',
      badge: 120,
      name: '会员卡',
      url: "../menberCard/menberCard",
      function: "",
      type: "page",
    }, {
      icon: 'icon-chongzhi',
      color: 'black',
      badge: 1,
      name: '充值中心',
      url: "../storedValue/storedValue",
      function: "",
      type: "page",
    }, {
      icon: 'icon-a-28',
      color: 'black',
      badge: 0,
      name: '我的订单',
      url: "../myOrder/myOrder",
      function: "",
      type: "page",
    }, {
      icon: 'icon-a-25',
      color: 'black',
      badge: 0,
      name: '我的地址',
      url: "../address/addressList/addressList",
      function: "",
      type: "page",
    }, {
      icon: 'icon-lingquanzhongxin',
      color: 'black',
      badge: 22,
      name: '领券中心',
      url: "../coupon/getCoupon/getCoupon",
      function: "",
      type: "page",
    }, {
      icon: 'icon-wode-tuijianyouli',
      color: 'black',
      badge: 0,
      name: '激活有礼',
      url: "../storedValue/storedValue",
      function: "",
      type: "page",
    }, {
      icon: 'icon-kaquan',
      color: 'black',
      badge: 0,
      name: '卡券商城',
      url: "../equityCardShop/equityCardShop",
      function: "",
      type: "page",
    }, {
      icon: 'icon-a-04',
      color: 'black',
      badge: 0,
      name: '积分商城',
      url: "../integralMall/integralMall",
      function: "",
      type: "page",
    }, {
      icon: 'icon-sharpicons_coffee-take-go',
      color: 'black',
      badge: 0,
      name: '集杯',
      url: "../collectionReward/collectionReward",
      function: "",
      type: "page",
    }, {
      icon: 'icon-cunqujiu',
      color: 'black',
      badge: 0,
      name: '存取酒',
      url: "../accessWine/accessWine",
      function: "",
      type: "page",
    }, {
      icon: 'icon-a-24',
      color: 'black',
      badge: 0,
      name: '联系商家',
      url: "",
      function: "callPhone",
      type: "function",
    }, {
      icon: 'icon-tuisong',
      color: 'black',
      badge: 0,
      name: '消息推送',
      url: "",
      function: "pushMessage",
      type: "function",
    }],
    userInfo: {
      activationTime: '',
      avatar: '',
      beid: 0,
      birthday: '',
      cardNumber: '',
      cardRemaining: 0,
      couponCount: 0,
      createBy: '',
      createTime: '',
      credit: 0,
      enable: 0,
      equityCount: 0,
      gender: 0,
      id: 0,
      idCard: '',
      isImport: 0,
      lastLoginTime: '',
      level: 0,
      mobile: '',
      nickname: '',
      onAccountStatus: 0,
      openVipCard: 1,
      openid: '',
      plat: '',
      realName: '',
      remark: '',
      sessionKey: '',
      unionId: '',
      updateBy: '',
      updateTime: '',
    },
    shopInfo: app.shopInfo,
    technicalSupportIcon: '',
    showNotice:false,
    picUrl:"",
  },
  getQrcode(){
    wx.showLoading({
      title: '加载中，请稍后……',
    })
    let that = this;
    let param = {
      memberId: app.myInfo.memberId     
    };
    getRequest("/api/vip/member/createBindQrcode", param).then((res) => {
      console.log(res.data.data)
      if (res.data.data != null) {
        that.setData({
          picUrl:res.data.data.picUrl,
          showNotice:true
        });
      }
      wx.hideLoading();
    }).catch((err) => {
      wx.hideLoading();
    });
  },
  hideNotice(){
    this.setData({
      showNotice:false
    })
  },
  //进入我的资料页
  onToMyInfoPage() {
    wx.navigateTo({
      url: '../myInfo/myInfo',
    })
  },

  //进入会员等级
  toMenberCardPage() {
    wx.navigateTo({
      url: '../menberCard/menberCard',
    })
  },

  //进入会员码页
  toPaymentCodePage() {
    if (this.data.userInfo.openVipCard == 1) {
      wx.navigateTo({
        url: '../paymentCode/paymentCode',
      })
    } else {
      this.onToCardRemainingPage();
    }
  },

  // 进入优惠券页
  onToMyCouponPage() {
    wx.navigateTo({
      url: '../coupon/myCoupon/myCoupon',
    })
  },

  //进入权益卡页
  onToEquityCardPage() {
    wx.navigateTo({
      url: '../equityCardShop/equityCardShop?dataType=1',
    })
  },

  // 进入储值页
  onToCardRemainingPage() {
    wx.navigateTo({
      url: '../storedValue/storedValue',
    })
  },

  //我的功能跳转页面
  onToFunPage(e) {
    //判断是页面跳转还是功能
    if (e.currentTarget.dataset.obj.type == "page") {
      if (e.currentTarget.dataset.obj.url != "") {
        wx.navigateTo({
          url: e.currentTarget.dataset.obj.url,
        })
      }
    } else {
      this[e.currentTarget.dataset.obj.function]();
    }

  },
  //电话联系门店
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.shopInfo.servicePhone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
 
  pushMessage(){
    this.getQrcode()
  },

  //跳转技术支持页面
  goApp() {
    wx.navigateToMiniProgram({
      appId: 'wx4ef45ca79b4ddfac',
      path: 'pages/index1/index1',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

  //获取个人信息
  getUserBaseInfo() {
    let that = this;

    //技术支持图片
    var imgIcon = wx.getStorageSync('shopInfo');
    if (imgIcon != undefined && imgIcon != '') {
      that.setData({
        technicalSupportIcon: imgIcon.technicalSupportIcon,
      })
    }

    getRequest("/api/vip/getUserBaseInfo", {
      memberId: app.myInfo.memberId
    }).then((res) => {
      console.log(res.data.data);
      console.log(that.data);
      if (res.data.data != null) {
        wx.setStorageSync('userInfo', res.data.data);
        if (res.data.data.avatar == '') {
          if (res.data.data.gender == 2) {
            res.data.data.avatar = that.data.myIndexHeadImg2;
          } else {
            res.data.data.avatar = that.data.myIndexHeadImg1;
          }
        }
        that.setData({
          userInfo: res.data.data,
          showStyle: 1,
        })
      }
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
    }).catch((err) => {
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
      that.setData({
        showStyle: 3,
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.getUserBaseInfo();
      }).catch(err => {})
    } else {
      this.getUserBaseInfo();
    }
    editTabBar(this, 'personal');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserBaseInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})